//
//  ZQComposeToolbar.m
//  柳州微博
//
//  Created by Apple_Lzzy45 on 16/11/7.
//  Copyright © 2016年 Apple_Lzzy40. All rights reserved.
//

#import "ZQComposeToolbar.h"

@interface ZQComposeToolbar ()
@property(nonatomic,strong) UIButton *Emontion;
@end

@implementation ZQComposeToolbar
-(instancetype)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if (self) {
        //
        self.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"compose_toolbar_background"]];
        //相册
        [self setupBtnWithNormalImage:@"compose_toolbar_picture"  HighlightedImage:@"compose_toolbar_picture_highlighted"buttonType:ZQComposeToolbarButtonTypePicture];
        
        //@
         [self setupBtnWithNormalImage:@"compose_mentionbutton_background"  HighlightedImage:@"compose_mentionbutton_background_highlighted"buttonType:ZQComposeToolbarButtonTypeMention];
        
        //#
         [self setupBtnWithNormalImage:@"compose_trendbutton_background"  HighlightedImage:@"compose_trendbutton_background_highlighted"buttonType:ZQComposeToolbarButtonTypeTrend];
        //表情
         [self setupBtnWithNormalImage:@"compose_emoticonbutton_background"  HighlightedImage:@"compose_emoticonbutton_background_highlighted"buttonType:ZQComposeToolbarButtonTypeEmontion];
        //拍照
         [self setupBtnWithNormalImage:@"compose_camerabutton_background"  HighlightedImage:@"compose_camerabutton_background_highlighted"buttonType:ZQComposeToolbarButtonTypeCamera];
        
    }
    return self;
}
//创建按钮
-(void)setupBtnWithNormalImage:(NSString *)image HighlightedImage:(NSString *)highImage  buttonType:(ZQComposeToolbarButtonType)type
{

    //初始化按钮
    UIButton * btn =[[UIButton alloc]init];
    
    //常态图片
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    
    //高亮图片
    [btn setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    
    btn.tag =type;
    if (type == ZQComposeToolbarButtonTypeEmontion) {
        self.Emontion = btn;
    }
    //添加点击事件
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];

}
-(void)layoutSubviews
{
    [super layoutSubviews];
    //设置所有按钮的frame
    NSUInteger count = self.subviews.count;
    CGFloat btnW = self.width /count;
    CGFloat btnH = self.height;
    for (NSUInteger i =0; i<count; i++) {
        UIButton *btn =self.subviews[i];
        btn.x =i*btnW;
        btn.y =0;
        btn.width =btnW;
        btn.height =btnH;
    }
    
}
-(void)btnClick:(UIButton *)btn
{

    
    //CGFloat btnX = btn.x;
    
    ZQComposeToolbarButtonType num =btn.tag;
    //NSLog(@"%ld",num);
    //调用代理方法
    if ([self.delegate respondsToSelector:@selector(composeToobar: didClickBtn:)]) {
        [self.delegate composeToobar:self didClickBtn:num];
    }
    
}
-(void)setKeyExpression:(BOOL)keyExpression
{
    _keyExpression = keyExpression;
    
    if (_keyExpression) {
        [self.Emontion setImage:[UIImage imageNamed:@"compose_keyboardbutton_background"] forState:UIControlStateNormal];
        [self.Emontion setImage:[UIImage imageNamed:@"compose_keyboardbutton_background_highlighted"] forState:UIControlStateHighlighted];
    } else {
        [self.Emontion setImage:[UIImage imageNamed:@"compose_emoticonbutton_background"] forState:UIControlStateNormal];
        [self.Emontion setImage:[UIImage imageNamed:@"compose_emoticonbutton_background_highlighted"] forState:UIControlStateHighlighted];
    }
}

@end
